package com.test;

import java.util.List;

import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.ProcessEngineImpl;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.Job;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

public class SignalThrowingEvent {
	@org.junit.Test
	public void test() {
		// 创建一个流程数据源的配置
		ProcessEngine processEngine = ProcessEngineConfiguration
				.createProcessEngineConfigurationFromResource(
						"temp/activiti.cfg.xml").buildProcessEngine();
		ProcessEngineImpl engine = (ProcessEngineImpl) processEngine;
		// 启动JobExecutor
//		engine.getProcessEngineConfiguration().getJobExecutor().start();
		// 获取数据源连接的服务
		RepositoryService repositoryService = processEngine
				.getRepositoryService();
		RuntimeService runtimeService = processEngine.getRuntimeService();
		TaskService taskService = processEngine.getTaskService();
		ManagementService managementService = processEngine
				.getManagementService();
		// 读取配置流程配置文件对象
		Deployment deployment = repositoryService.createDeployment()
				.addClasspathResource("diagrams/SignalThrowingEvent.bpmn")
				.deploy();
		ProcessInstance processInstance = runtimeService
				.startProcessInstanceByKey("stProcess");
		Task currentTask = taskService.createTaskQuery()
				.processInstanceId(processInstance.getId()).singleResult();
		//完成选择商品任务
		taskService.complete(currentTask.getId());
		currentTask=taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
		//完成用户支付任务
		taskService.complete(currentTask.getId());
		List<Job> jobs=managementService.createJobQuery().list();
		System.out.println(jobs.size());
		currentTask=taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
		System.out.println(currentTask.getName());
	}
}
